using System;

namespace XTCast.Mp3.PCM
{
	/// <summary>
	/// Summary description for PCMUtil.
	/// </summary>
	public class PCMUtil
	{
		private PCMUtil()
		{
		}

		public static void MonoToStereo(byte[] inBytes, int inSize, byte[] outBytes, int outSize) {
			if (inSize*2>outSize) 
				throw new ArgumentException("Out buffer size must be at least twice the in buffer size");
			int j=0;
			for (int i=0;i<inSize;i+=2) {
				outBytes[j]=inBytes[i];
				outBytes[j+1]=inBytes[i+1];
				outBytes[j+2]=inBytes[i];
				outBytes[j+3]=inBytes[i+1];
				j+=4;
			}
		}

		public static void StereoToMono(byte[] inBytes, int inSize, byte[] outBytes, int outSize) {
			if (inSize>outSize*2)
				throw new ArgumentException("Out buffer size must be at least half the in buffer size");
			int j=0;
			for (int i=0;i<inSize;i+=4) {
				/*outBytes[j]=inBytes[i];
				outBytes[j+1]=inBytes[i+1];*/
				short sampl = (short)((inBytes[i])|(inBytes[i+1]<<8));
				short sampr = (short)((inBytes[i+2])|(inBytes[i+3]<<8));
				int sampm = (sampl+sampr)/2;
				outBytes[j]=(byte)(sampm & 0xFF);
				outBytes[j+1]=(byte)((sampm>>8) & 0xFF);
				j+=2;
			}
		}
	}
}
